package com.qzw.rabbitmq.common.service.impl;

import com.qzw.rabbitmq.common.BaseExchangeEnum;
import com.qzw.rabbitmq.common.BaseQueueEnum;
import com.qzw.rabbitmq.common.service.QueueMessageService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author ：quziwei
 * @date ：Created in 2020/6/3 3:04 下午
 * @description：
 */

@Component
public class QueueMessageServiceSupport implements QueueMessageService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void send(Object message, BaseExchangeEnum exchangeEnum, BaseQueueEnum queueEnum) {
        rabbitTemplate.setConfirmCallback(this);
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(exchangeEnum.getValue(),queueEnum.getRoutingKey(),message,correlationData);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println(" 回调id:" + correlationData.getId());
        if (ack) {
            System.out.println("消息发送成功");
        } else {
            System.out.println("消息发送失败:" + cause);
        }
    }
}
